package boundary;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Frame;
import javax.swing.JDialog;
import dataview.TipoTermine;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Toolkit;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.jdesktop.swingx.JXTitledSeparator;
import store.Termine;
import javax.swing.JScrollPane;

public class DialogTermine extends JDialog implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private TipoTermine tipo;
	private JPanel buttonBar = null;
	private MyJTree tree = null;
	private JButton okB = null;
	private JButton annullaB = null;
	private JXTitledSeparator titleS = null;
	private Termine value = null;
	private JScrollPane jScrollPane = null;
	private boolean obbligatorio;
	private boolean selezionaSoloFoglie;
	
	/**
	 * @param owner
	 */
	public DialogTermine(Frame owner, TipoTermine tipo, boolean obbligatorio, boolean selezionaSoloFoglie) {
		super(owner);
		this.tipo = tipo;
		this.obbligatorio = obbligatorio;
		this.selezionaSoloFoglie = selezionaSoloFoglie;
		setModal(true);
		setTitle("Selezione " + tipo.toString().replace('_', ' '));
		initialize();
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((d.width - 227) / 2, (d.height - 255) / 2);
		setLocationRelativeTo(owner);
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(227, 255);
		this.setPreferredSize(new Dimension(227, 255));
		this.setContentPane(getJContentPane());
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
			gridBagConstraints41.fill = GridBagConstraints.BOTH;
			gridBagConstraints41.weighty = 1.0;
			gridBagConstraints41.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints41.gridx = 0;
			gridBagConstraints41.gridy = 1;
			gridBagConstraints41.weightx = 1.0;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.gridy = 2;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getButtonBar(), gridBagConstraints);
			jContentPane.add(getTitleS(), gridBagConstraints4);
			jContentPane.add(getJScrollPane(), gridBagConstraints41);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes buttonBar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonBar() {
		if (buttonBar == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.insets = new Insets(0, 0, 0, 5);
			gridBagConstraints3.gridy = 0;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.insets = new Insets(0, 5, 0, 0);
			gridBagConstraints2.gridy = 0;
			buttonBar = new JPanel();
			buttonBar.setLayout(new GridBagLayout());
			buttonBar.add(getOkB(), gridBagConstraints3);
			buttonBar.add(getAnnullaB(), gridBagConstraints2);
		}
		return buttonBar;
	}
	
	/**
	 * This method initializes tree
	 * 
	 * @return org.jdesktop.swingx.JXTree
	 */
	private MyJTree getTree() {
		if (tree == null) {
			tree = new MyJTree(tipo, selezionaSoloFoglie);
			tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
				public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
					getOkB().setEnabled(getTree().getSelectedTermine() != null);
				}
			});
		}
		return tree;
	}
	
	/**
	 * This method initializes okB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkB() {
		if (okB == null) {
			okB = new JButton();
			okB.setEnabled(false);
			okB.setText("Conferma");
			okB.addActionListener(this);
		}
		return okB;
	}
	
	/**
	 * This method initializes annullaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAnnullaB() {
		if (annullaB == null) {
			annullaB = new JButton();
			annullaB.setText("Annulla");
			annullaB.addActionListener(this);
		}
		return annullaB;
	}
	
	/**
	 * This method initializes titleS
	 * 
	 * @return org.jdesktop.swingx.JXTitledSeparator
	 */
	private JXTitledSeparator getTitleS() {
		if (titleS == null) {
			titleS = new JXTitledSeparator();
			titleS.setTitle("Seleziona un termine");
		}
		return titleS;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == getOkB()) {
			value = getTree().getSelectedTermine();
			if (!obbligatorio)
				if (value == null) {
					if (JOptionPane.showConfirmDialog(this, "Non e' stato selezionato nessun termine. Procedere?", "Nessun termine selezionato", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION)
						return;
				}
			setVisible(false);
		} else if (e.getSource() == getAnnullaB()) {
			dispose();
		}
	}
	
	public Termine getTermine() {
		return value;
	}
	
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getTree());
		}
		return jScrollPane;
	}
	
} //  @jve:decl-index=0:visual-constraint="10,10"
